
$(function(){
	//初始化列表查询
    paging(1);

    //1 绑定添加按钮
    $('.addbtn').click(function(){
        layer.open({
        	extend:['skin/layer_iframe.css'],
        	skin:'layer-ext-skin-layer_iframe.css',
            type:2,
            title:'添加页面',
            area:['600px','400px'],
            content:'initHostGroup.do',
            maxmin: true,
            shadeClose: true
        });
    });
    
    //4 绑定查询按钮
    $('.sel_btn').click(function(){
    	paging(1);
    });
    
    //5 绑定重置按钮
    $('.reset_btn').click(function(){
    	$("#personName").val('');
    	$("#personName").focus();
    });
    //6.删除选定的，多项删除
    $('.delbtn').click(function(){
    	del_select_func(this);
    });
});



checkallmethod=function(){
    $('.checkAll').click(function(){
        if($(this).is(':checked')){
            $('.content_table .checkinput').attr('checked','checked');
            $(this).attr('title','取消全选')
        }else{
            $('.content_table .checkinput').removeAttr('checked');
            $(this).attr('title','全选')
        }
    })
}

function paging(pageNo){
	var pageSize = $("#pageSize  option:selected").text();
    $.ajax({
        url: "pagingHostGroup.do",
        type: "post",
        dataType: 'json',
        async: false,
        data: {
        	"iPageSize": pageSize,
            "iPageNo": pageNo
        },
        error: function (data) {
        	layer.msg('出错了！！',{time:2000});
        },
        success: function (jsons) {
        	if(jsons.code == 500){
        		//alert(jsons.eroorMessage);
                layer.msg(jsons.errorMessage,{time:2000});
        	}else if (jsons.code == 201){
        		//alert("数据为空！");
                layer.msg("数据为空！",{time:2000});
        	}else{
        	    //给“主机个数”和“模板个数”绑定上超链接
        		unifiedPaging(jsons.pageData);
        		addHyperlink(jsons.pageData);
        	}
        }
    })
    checkallmethod();
}

function addHyperlink(pageData){
	var pageList = pageData.pageList;
	for(var i =0, len=pageList.length;i<=len;i++){
		add(i,2, webPath+"/radar/host/menuHost.do");
		add(i,3, webPath+"/radar/template/menuTemplate.do");
	}
}
function add(rowIndex, cellIndex, url){
	var groupid=$("tr:eq("+rowIndex+")").attr("ID");
	var href = url+"?groupid="+groupid;
	$x = $("tr:eq("+rowIndex+")").find("td").eq(cellIndex).wrapInner("<a href="+href+"></a>");
}

//see_btn=function(obj){
//    //绑定查看按钮
//	var Id=$(obj).parent().parent().attr("ID");
//	layer.open({
//		extend:['skin/layer_iframe.css'],
//        skin:'layer-ext-skin-layer_iframe.css',
//        type:2,
//        title:'查看主机和模板',
//        area:['800px','600px'],
//        content:'showHostsAndTemplates.do?groupid='+Id,
//        maxmin: true,
//        shadeClose: true
//    });
//}

/**
 * 点击列表右侧修改按钮
 */
edit_func=function(obj){
    var Id=$(obj).parent().parent().attr('ID');
    layer.open({
    	extend:['skin/layer_iframe.css'],
        skin:'layer-ext-skin-layer_iframe.css',
        type:2,
        title:'修改页面',
        area:['600px','400px'],
        content:'initEditHostGroup.do?groupid='+Id,
        maxmin: true,
        shadeClose: true
    });
}

/**
 * 点击右侧删除按钮
 * @param obj
 */
delete_func=function(obj){
    var Id=$(obj).parent().parent().attr('ID');
    if(confirm('确定删除?')){
        $.ajax({
            url:"delHostGroup.do",
            type:"post",
            dataType: 'json',
            data:{
                "groupid":Id
            },
            beforeSend:function(){
                $(obj).attr("disabled","disabled");
            },
            error:function(data){
            	layer.msg('出错了！！',{time:2000});
            },
            success:function(result){
                if(result.code=='200'){
                    layer.msg(result.errorMessage, {time:1300});
                    $('.checkAll').removeAttr('checked');
                    paging(1);
                }else{
                    layer.msg(result.errorMessage, {time:1000});
                    $(obj).removeAttr("disabled");
                }
            }
        });
    }
}

function del_select_func(obj){
	var checkedDel = "";
	 var arr=document.getElementsByName("checkradio");
	 	
	 	for(i=0;i<arr.length;i++){
			if(arr[i].checked){
			var a = arr[i].value;
			checkedDel+=a+",";
			}
		}
		if(checkedDel==""){
			layer.msg('先勾选选择框，再点击删除按钮！', {time:1300});
		}else{
			if(confirm('确定删除所有选中关系')){
			$.ajax({
            url:"delSelectHostGroup.do",
            type:"post",
            dataType: 'json',
            data:{
                "checkedDel":checkedDel
            },
            beforeSend:function(){
                
            },
            error:function(data){
            	layer.msg('出错了！！',{time:2000});
            },
            success:function(result){
                if(result.code=='200'){
                    layer.msg(result.errorMessage, {time:1300});
                    $('.checkAll').removeAttr('checked');
                    paging(1);
                }else{
                    layer.msg(result.errorMessage, {time:1000});
                    $(obj).removeAttr("disabled");
                }
            }
        });
			}
		}
}


